/*
 * @Description  : 音频管理
 * @Author       : YiDing Xue
 * @Date         : 2023-02-28
 * @LastEditors  : YiDing Xue
 * @LastEditTime : 2023-03-10
 */
import { defineStore } from 'pinia'
import { isEmpty } from '~/src/utils/common'
import { audioSource } from '~/src/api/works'

export const useAudioStore = defineStore('audio', {
  state() {
    return {
      audioRef: null,
      sourceRef: null,
      miniPlayerShow: false, // 迷你播放器显示
      playStatus: false, // 播放状态
      // 当前播放的音频
      playInfo: {
        albumId: '',
        sort: 1,
        imgUrl: '',
        pathUrl: '',
        episodeName: '',
        author: '',
        introduction: '',
        seconds: 7,
        formatSeconds: '',
        startTime: 0,
        endTime: 0,
        totalEpisode: 0
      }
    }
  },
  actions: {
    /**
     * 初始化 audio source
     * @param {object} audioRef audio DOM 节点
     * @param {object} sourceRef source DOM 节点
     */
    setAudioAndSource(audioRef, sourceRef) {
      this.audioRef = audioRef
      this.sourceRef = sourceRef
    },
    /**
     * 设置播放状态
     * @param {Boolean} flag 播放状态 true-播放 false-停止
     */
    async setPlayStatus(playStatus) {
      if (isEmpty(this.sourceRef.src)) {
        const { data: result } = await audioSource(this.playInfo.albumId, this.playInfo.sort)
        if (!result) {
          return $message.error('资源获取失败,请联系客服')
        }
      }
      this.playStatus = playStatus
      this.miniPlayerShow = true
      // this.playTime = this.playInfo.endTime || 0
      playStatus ? this.audioRef.play() : this.audioRef.pause()
    },
    /**
     * 设置迷你播放器显示
     * @param {Boolean} flag 迷你播放播放器显示 true-显示 false-消失
     */
    setMiniPlayerShow(flag) {
      this.miniPlayerShow = flag
    },
    /**
     * 设置播放对象
     * @param {Object} playInfo b播放对象
     */
    setPlayInfo(playInfo) {
      this.playInfo = playInfo
      this.sourceRef.src = playInfo.pathUrl
      this.audioRef.currentTime = playInfo.endTime || 0
      // 当刷新了url之后 需要执行load方法才能播放
      this.audioRef.load()
    },
    /**
     * 设置播放时间
     * @param {Number} playTime 播放时间
     */
    setPlayTime(playTime) {
      this.playInfo.endTime = playTime
    },
    /**
     * 上一集 下一集
     * @param {Boolean} episodeFlag true-上一集 false-下一集
     */
    async switchEpisode(episodeFlag) {
      // 当前集数
      let sort = this.playInfo.sort
      // 总集数
      let total = this.playInfo.totalEpisode

      if (this.playInfo.sort === 1 && episodeFlag) {
        return $message?.warning('当前是第一集')
      }

      if (!episodeFlag && sort === total) {
        return $message?.warning('您听到最后一集了')
      }

      // 集数增减
      episodeFlag ? sort-- : sort++

      try {
        const { data: result } = await audioSource(this.playInfo.albumId, sort)
        // 剧集信息为 null 暂停 回到开头
        if (isEmpty(result)) {
          this.setPlayStatus(false)
          this.audioRef.currentTime = 0
          return $message?.warning('解锁下一章请进入我的页面联系客服')
        }
        this.setPlayInfo(result)
      } catch (error) {
        this.setPlayStatus(false)
        return $message?.error(error)
      }
    },
    /**
     * 设置倍速播放速度
     * @param {Object} playbackRate 播放速度
     */
    setPlaybackRate(playbackRate) {
      this.audioRef.playbackRate = playbackRate
    },
    /** 前进 */
    fn1(playInfo) {
      this.playInfo = playInfo
    },
    // 后退
    fn1(playInfo) {
      this.playInfo = playInfo
    }
  }
})
